package dao.impl;

import dao.AdRossDao;
import pojo.AdRoss;
import util.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 管理员角色关联接口的实现类
 */
public class AdRossDaoImpl implements AdRossDao {

    /**
     * 添加一个管理员角色关联
     */
    @Override
    public int inseUrass(AdRoss adRoss) {
        return BaseDao.cud("insert into adross values(sequence_adross.nextval,?,?)",adRoss.getAdminId(),adRoss.getRoleId());
    }

    /**
     *根据管理员id查找管理员角色对
     */
    @Override
    public AdRoss queryUrass(int adminId) {
        AdRoss adRoss=new AdRoss();
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;

        try {
            conn=BaseDao.getConnection();
            String sql="select * from adross where admin_id=?";
            ps=conn.prepareStatement(sql);
            ps.setInt(1,adminId);
            rs=ps.executeQuery();
            while (rs.next()){
                adRoss.setAdRoId(rs.getInt("adro_id"));
                adRoss.setAdminId(rs.getInt("admin_id"));
                adRoss.setRoleId(rs.getInt("role_id"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.release(rs,ps,conn);
        }
        return adRoss;
    }

    /**
     *根据管理员id修改管理员角色对
     */
    @Override
    public int updateUrass(int adminId, int roleId) {
        return BaseDao.cud("update adross set role_id=? where admin_id=?",roleId,adminId);
    }

    /**
     *根据管理员id删除管理员角色对
     */
    @Override
    public int deleUrass(int adminId) {
        return BaseDao.cud("delete from adross where admin_id=?",adminId);
    }
}
